package com.bhprod.comatule.index;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhprod.comatule.MenuActivity;
import com.bhprod.comatule.R;
import com.bhprod.comatule.db.DBDataSource;
import com.bhprod.comatule.detail.SpeciesDetailActivity;
import com.bhprod.comatule.model.Species;

public class IndexActivity extends Activity {

	/**
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_index);

		handleIntent(getIntent());
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, MenuActivity.class);
		startActivity(intent);
		finish();
	}

	/**
	 * @see android.app.Activity#onNewIntent(android.content.Intent)
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		handleIntent(intent);
	}

	public void launchSearch(View view) {
		onSearchRequested();
	}

	public void displayFrenchList(View view) {
		displayList(DBDataSource.getInstance().getSpeciesSortedByFrenchName(), false);
	}

	public void displayLatinList(View view) {
		displayList(DBDataSource.getInstance().getSpeciesSortedByLatinName(), true);
	}

	public void displaySearchList(String query) {
		displayList(DBDataSource.getInstance().searchSpeciesSortedByFrenchName(query), false);
	}

	private void displayList(Map<String, List<String[]>> names, boolean latinFirst) {
		LinearLayout layout = (LinearLayout) findViewById(R.id.indexZone);

		layout.removeAllViews();
		
		LayoutInflater infalInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if (names.isEmpty()) {
			TextView nameLayout = (TextView) infalInflater.inflate(R.layout.index_data, null);
			nameLayout.setText(R.string.no_result);

			layout.addView(nameLayout);
		} else {
			for (String letter : names.keySet()) {
				TextView letterLayout = (TextView) infalInflater.inflate(R.layout.index_letter, null);
				letterLayout.setText(letter.toUpperCase(getResources().getConfiguration().locale));
	
				layout.addView(letterLayout);
	
				for (String[] allNames : names.get(letter)) {
					LinearLayout namesLayout = (LinearLayout) infalInflater.inflate(R.layout.index_data, null);
					TextView firstName = (TextView) namesLayout.getChildAt(0);
					firstName.setText(allNames[0] + " ");
					if (latinFirst) {
						firstName.setTextAppearance(this, R.style.italicText);
					}
	
					TextView secondName = (TextView) namesLayout.getChildAt(1);
					secondName.setText("(" + allNames[1] + ")");
					if (!latinFirst) {
						secondName.setTextAppearance(this, R.style.italicText);
					}
					
					TextView hiddenId = (TextView) namesLayout.getChildAt(2);
					hiddenId.setText(allNames[2]);

					layout.addView(namesLayout);
				}
			}
		}
	}

	private void handleIntent(Intent intent) {
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			String query = intent.getStringExtra(SearchManager.QUERY);
			displaySearchList(query);
		} else {
			displayFrenchList(null);
		}
	}
	
	public void displayDetail(View view) {
		TextView hiddenId = (TextView) (((LinearLayout)view).getChildAt(2));
		Species currentSpecies = DBDataSource.getInstance().getSpeciesById(Integer.valueOf(hiddenId.getText().toString()));
		Intent intent = new Intent(this, SpeciesDetailActivity.class);
		Bundle b = new Bundle();
		b.putInt("speciesId", currentSpecies.getId());
		intent.putExtras(b);
		startActivity(intent);
	}

}
